package com.hckj.jianzhijia.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.hckj.jianzhijia.EnrollPeopleInfoActivity;
import com.hckj.jianzhijia.R;
import com.hckj.jianzhijia.adapters.EnrollPeopleListAdapter;
import com.hckj.jianzhijia.model.Job;
import com.hckj.jianzhijia.model.NormalUser;
import com.hckj.jianzhijia.utils.ExceptionUtils;

import java.util.ArrayList;
import java.util.List;

public class EnrollPeopleFragment extends ListViewFragment {

    private static final String ARG_PARAM = "job";
    private Job job;
    private OnFragmentInteractionListener mListener;
    private ArrayList<NormalUser> avUserArrayList;
    private int[] colorResource = {R.color.color_primary};
    private EnrollPeopleListAdapter enrollPeopleListAdapter;
    private GetEnrollPeopleTask getEnrollPeopleTask;

    public EnrollPeopleFragment() {
    }

    public static EnrollPeopleFragment newInstance(AVObject avObject) {
        EnrollPeopleFragment fragment = new EnrollPeopleFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM, avObject);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            job = new Job((AVObject) getArguments().getParcelable(ARG_PARAM));
        }
        avUserArrayList = new ArrayList<NormalUser>();
        enrollPeopleListAdapter = new EnrollPeopleListAdapter(getActivity(), avUserArrayList);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        getEnrollPeopleTask = new GetEnrollPeopleTask();
        getEnrollPeopleTask.execute();
        return view;
    }

    @Override
    public void afterUpdateListFresh() {

    }

    @Override
    public BaseAdapter getListAdapter() {
        return enrollPeopleListAdapter;
    }

    @Override
    public boolean useCustomContentView() {
        return false;
    }

    @Override
    public int getCustomContentView() {
        return 0;
    }

    @Override
    public boolean pullToRefreshEnabled() {
        return true;
    }

    @Override
    public int[] getPullToRefreshColorResources() {
        return colorResource;
    }

    @Override
    public void onRefresh() {
        getEnrollPeopleTask = new GetEnrollPeopleTask();
        getEnrollPeopleTask.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Intent intent = new Intent(getActivity(), EnrollPeopleInfoActivity.class);
        intent.putExtra("NormalUser", avUserArrayList.get(position));
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        return false;
    }


    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction();
    }

    class GetEnrollPeopleTask extends AsyncTask<Void, Void, List<NormalUser>> {

        @Override
        protected List<NormalUser> doInBackground(Void... params) {


            try {
                //暂时不进行封装
                String jobId = job.getObjectId();
                List<AVUser> avUsers = new ArrayList<AVUser>();
                List<NormalUser> normalUsers = new ArrayList<NormalUser>();
                AVQuery<AVObject> avObjectAVQuery = new AVQuery<AVObject>("enrollJob");
                AVQuery<AVUser> avUserAVQuery = AVUser.getQuery();
                AVQuery<AVObject> normalUsersAVQuery = new AVQuery<AVObject>(NormalUser.TABLE_NAME);
                avObjectAVQuery.whereEqualTo("jobId", jobId);
                List<AVObject> list = avObjectAVQuery.find();
                for (AVObject enrollJob : list) {
                    String phone = enrollJob.getString("userPhoneNumber");
                    System.out.println("phone  " + phone);
                    avUserAVQuery.whereEqualTo("username", phone);
                    normalUsersAVQuery.whereEqualTo("phoneNumber", phone);
                    AVUser tempAvUser = avUserAVQuery.find().get(0);
                    AVObject tempObject = normalUsersAVQuery.find().get(0);//开启的网络请求过多了
                    NormalUser normalUser = new NormalUser(tempAvUser, tempObject);
                    normalUsers.add(normalUser);
                }
                return normalUsers;
            } catch (AVException e) {
                e.printStackTrace();
                System.out.println(ExceptionUtils.getExceptionInfo(e));
                return null;
            }
        }

        @Override
        protected void onPostExecute(List<NormalUser> s) {
            if (null == s) {
                s = new ArrayList<NormalUser>();
            }
            avUserArrayList.clear();
            avUserArrayList.addAll(s);
            enrollPeopleListAdapter.notifyDataSetChanged();
            setRefreshing(false);
            super.onPostExecute(s);
        }

        @Override
        protected void onPreExecute() {
            setRefreshing(true);
            super.onPreExecute();
        }
    }

}
